package com.Pawtrack.web.controller.info;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.Pawtrack.common.annotation.Log;
import com.Pawtrack.common.annotation.DataScope;
import com.Pawtrack.common.core.controller.BaseController;
import com.Pawtrack.common.core.domain.AjaxResult;
import com.Pawtrack.common.enums.BusinessType;
import com.Pawtrack.info.domain.PetReminder;
import com.Pawtrack.info.domain.PetHealth;
import com.Pawtrack.info.service.IPetReminderService;
import com.Pawtrack.info.service.IPetHealthService;
import com.Pawtrack.info.utils.UUIDGenerator;
import com.Pawtrack.common.utils.poi.ExcelUtil;
import com.Pawtrack.common.core.page.TableDataInfo;
import com.Pawtrack.common.utils.SecurityUtils;

/**
 * 宠物提醒Controller
 * 
 * @author ruoyi
 * @date 2025-08-14
 */
@RestController
@RequestMapping("/system/reminder")
public class PetReminderController extends BaseController
{
    @Autowired
    private IPetReminderService petReminderService;
    
    @Autowired
    private IPetHealthService petHealthService;

    /**
     * 查询宠物提醒列表
     */
    @GetMapping("/list")
    @DataScope(deptAlias = "p", userAlias = "p")
    public TableDataInfo list(PetReminder petReminder)
    {
        startPage();
        List<PetReminder> list = petReminderService.selectPetReminderList(petReminder);
        return getDataTable(list);
    }

    /**
     * 导出宠物提醒列表
     */
    @Log(title = "宠物提醒", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PetReminder petReminder)
    {
        List<PetReminder> list = petReminderService.selectPetReminderList(petReminder);
        ExcelUtil<PetReminder> util = new ExcelUtil<PetReminder>(PetReminder.class);
        util.exportExcel(response, list, "宠物提醒数据");
    }

    /**
     * 获取宠物提醒详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(petReminderService.selectPetReminderById(id));
    }

    /**
     * 新增宠物提醒
     */
    @Log(title = "宠物提醒", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PetReminder petReminder)
    {
        // 设置创建人为当前用户ID
        petReminder.setCreateBy(String.valueOf(SecurityUtils.getUserId()));
        
        // 生成唯一ID
        String uuid = UUIDGenerator.generate();
        petReminder.setId(uuid);
        
        // 插入提醒记录
        int result = petReminderService.insertPetReminder(petReminder);
        
        // 返回结果和生成的ID
        AjaxResult ajaxResult = toAjax(result);
        ajaxResult.put("reminderId", uuid);
        return ajaxResult;
    }

    /**
     * 修改宠物提醒
     */
    @Log(title = "宠物提醒", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PetReminder petReminder)
    {
        // 设置更新人为当前用户ID
        petReminder.setUpdateBy(String.valueOf(SecurityUtils.getUserId()));
        return toAjax(petReminderService.updatePetReminder(petReminder));
    }

    /**
     * 删除宠物提醒
     */
    @Log(title = "宠物提醒", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(petReminderService.deletePetReminderByIds(ids));
    }
    
    /**
     * 更新健康记录的提醒ID
     */
    @Log(title = "更新健康记录提醒ID", businessType = BusinessType.UPDATE)
    @PutMapping("/updateHealthReminder")
    public AjaxResult updateHealthReminder(@RequestBody PetHealth petHealth)
    {
        // 设置更新人为当前用户ID
        petHealth.setUpdateBy(String.valueOf(SecurityUtils.getUserId()));
        return toAjax(petHealthService.updatePetHealth(petHealth));
    }
}